dojo.require("dojox.av.FLVideo");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.Slider"); 
dojo.require("dojo.parser");


var player, controls, progress, lib, libNode;
var dndItem = {};



dojo.addOnLoad(function(){
	console.count("video")
	
	
	player =  new dair.Video({}, "video");//new dojox.av.FLVideo({initialVolume:.2, isDebug:true}, "video");
	
	
	dojo.connect(player, "onLoad", controls, "init");
	//dojo.connect(player, "onClick", player, "togglePause");
	dojo.connect(player, "onMetaData", controls.progress, "onMeta");
	dojo.connect(player, "onEnd", controls, "onVideoEnd");
	dojo.connect(player, "onStart", controls, "onVideoStart");
	console.log("PLAYER CONNECTED")
	/**/
});


controls = {
	progress: {
		init: function(){ console.warn("*************INIT*********");
			this.ObjectId = "controls"
			this.duration = null;
			this.seeking = false;
			this.progressBar = dijit.byId("progress");
			dojo.connect(this.progressBar.sliderHandle, "mousedown", this, "startDrag");
			this.timeNode = dojo.byId("timeNode");
			this.durNode = dojo.byId("durNode");
			dojo.connect(player, "onPosition", this, "onPosition");
			//setInterval(dojo.hitch(this, "watchTime"), 1000);
		},
		onMeta: function(data){
			if(data && data.duration){
				this.duration = data.duration;
				this.durNode.innerHTML = this.timecode(this.duration);
				this.progressBar.attr("disabled", false);
			}else{
				this.duration = null;
				this.durNode.innerHTML = "NA";
				this.progressBar.attr("disabled", true);
				this.progressBar.attr("value", 0);
			}
		},
		onDrag: function(val){
			//
			if(this.seeking){
				//console.log("DRAG:", val);
				var p = val*.01
				player.seek(this.duration*p);
			}
		},
		startDrag: function(){
			this.seeking = true;
			this.conChg = dojo.connect(this.progressBar, "onChange", this, "onDrag");
			this.conUp = dojo.connect(dojo.doc, "mouseup", this, "endDrag")
		},
		endDrag: function(){
			this.seeking = false;
			dojo.disconnect(this.conUp);
			dojo.disconnect(this.conChg);
		},
		onPosition: function(time){
			this.timeNode.innerHTML = this.timecode(time);
			if(this.duration){
				if(!this.seeking){
					var p = time/this.duration;
					this.progressBar.attr("value", p*100);
				}
			}
		},
		timecode: function(time){
			ts = time.toString()

			if(ts.indexOf(".")<0){
				ts += ".00"
			}else if(ts.length - ts.indexOf(".")==2){
				ts+="0"
			}else if(ts.length - ts.indexOf(".")>2){
                            ts = ts.substring(0, ts.indexOf(".")+3)
                        }
			return ts;
		}
	},
	volume: {
		
		init:function(){
			
			this.volNode = dojo.byId("volume");
			this.volBack = dojo.byId("volBack");
			dojo.setSelectable(this.volNode, false);
			dojo.setSelectable(this.volBack, false);
			dojo.setSelectable(dojo.byId("volMask"), false);
			
			this.volDim = dojo.coords(this.volNode);
			var v = player.initialVolume; // returns 0 - 1
			dojo.style(this.volBack, "backgroundPosition", "-"+(this.volDim.w-(this.volDim.w*v))+"px 0px");
			
			dojo.connect(this.volNode, "mousedown", this, "begDrag");
			dojo.connect(dojo.doc, "mouseup", this, "endDrag");
			dojo.connect(this.volNode, "mouseover", this, "over");
			dojo.connect(this.volNode, "mouseout", this, "out");
		},
		onDrag: function(evt){
			var x = evt.clientX - this.volDim.x;
			if(x<0) x = 0;
			if(x>this.volDim.w) x = this.volDim.w;
			var p = x/this.volDim.w;
			player.volume(p);
			var prex = x
			x = Math.ceil(x*.1)*10;
			dojo.style(this.volBack, "backgroundPosition", "-"+(this.volDim.w-x)+"px 0px");
		},
		begDrag: function(){
			this.dragging = true;
			this.conMove = dojo.connect(dojo.doc, "mousemove", this, "onDrag");
		},
		endDrag: function(){
			if(this.conMove) {
				dojo.disconnect(this.conMove);
			}
			this.dragging = false;
			this.out();
		},
		over: function(){
			dojo.addClass(this.volBack, "volBackHover");
		},
		out: function(){
			if(!this.dragging){
				dojo.removeClass(this.volBack, "volBackHover");	
			}
		}
	},
	
	
	init: function(){console.warn("*************INIT*********");
		this.progress.init();
		this.volume.init();
		this.initialized = true;
	},
	onVideoStart: function(){
		console.log("controls.onVideoStart");
		this.showPause();
	},
	onVideoEnd: function(){
		console.log("controls.onVideoEnd");
		this.showPlay();
	},
	doSlider: function(val){
		console.log("VALUE:", val);
	},
	load: function(path){
		
		if(!this.initialized){
			var c = dojo.connect(this, "init", this, function(){
				//this.load(path);
				dojo.disconnect(c);
			});
			return;
		}
		console.log("Play:", path)
		this.currentPath = path;
		player.play(path);
		this.showPause();
	},
	doPlay: function(){
		player.play();
		this.showPause();
	},
	doPause: function(){
		player.pause();
		this.showPlay();	
	},
	showPlay: function(){
		dojo.style(dijit.byId("pauseButton").domNode, "display", "none");
		dojo.style(dijit.byId("playButton").domNode, "display", "");
	},
	showPause: function(){
		dojo.style(dijit.byId("pauseButton").domNode, "display", "");
		dojo.style(dijit.byId("playButton").domNode, "display", "none");
	},
}